/*
 * @Description:   
 * @Author: YangJianFei
 * @Date: 2024-10-12 11:12:24
 * @LastEditTime: 2024-10-14 11:22:34
 * @LastEditors: YangJianFei
 * @FilePath: \yt_devop_app\lib\pages\devices\device_controll\component\rate_setting\rate_util.dart
 */

import 'package:collection/collection.dart';
import 'package:dart_date/dart_date.dart';
import 'package:get/get.dart';
import 'package:yt_devops/pages/devices/device_controll/component/rate_setting/rate_setting_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'package:yt_devops/utils/toast_utils.dart';

bool validate(
  List<TimeZoneData> timeZoneData,
  List<List<TimePeriodData>> timePeriodData,
) {
  int lastEffectIndex =
      timeZoneData.lastIndexWhere((item) => (item.Md ?? item.Tn) != null);
  for (int index = 0; index <= lastEffectIndex; index++) {
    TimeZoneData timeZone = timeZoneData[index];
    if (timeZone.Tn == null) {
      TotastUtils.tip(Get.context!,
          title:
              '${FuncUtils.formatMessage(resKey: ResKey.timeZoneTable, defaultMessage: '时区表')}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.timePeriodIsRequired, defaultMessage: '时段必填')}');
      return false;
    }
    if (timePeriodData[timeZone.Tn! - 1]
        .whereIndexed(
            (rowIndex, e) => (rowIndex == 0 ? e.m37 : (e.Hm ?? e.m37)) != null)
        .isEmpty) {
      TotastUtils.tip(Get.context!,
          title:
              '${FuncUtils.formatMessage(resKey: ResKey.timeZoneTable, defaultMessage: '时区表')}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.selectPeriodNotConfig, defaultMessage: '所选时段表未配置')}');
      return false;
    }
    if (timeZone.Md == null) {
      TotastUtils.tip(Get.context!,
          title:
              '${FuncUtils.formatMessage(resKey: ResKey.timeZoneTable, defaultMessage: '时区表')}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.dateIsRequired, defaultMessage: '日期必填')}');
      return false;
    }
    if (index > 0) {
      if (DateTime.parse('2024-${timeZone.Md}').isSameOrBefore(
          DateTime.parse('2024-${timeZoneData[index - 1].Md}'))) {
        TotastUtils.tip(Get.context!,
            title:
                '${FuncUtils.formatMessage(resKey: ResKey.timeZoneTable, defaultMessage: '时区表')}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.timeIsGreater, defaultMessage: '日期必须大于')}${timeZoneData[index - 1].Md}');
        return false;
      }
    }
  }

  for (int tableIndex = 0; tableIndex < timePeriodData.length; tableIndex++) {
    List<TimePeriodData> timePeriod = timePeriodData[tableIndex];
    int lastEffectIndex =
        timePeriod.lastIndexWhere((item) => (item.Hm ?? item.m37) != null);
    if (lastEffectIndex == 0) {
      continue;
    }
    for (int index = 0; index <= lastEffectIndex; index++) {
      TimePeriodData timePeriodRow = timePeriod[index];
      if (timePeriodRow.m37 == null) {
        TotastUtils.tip(Get.context!,
            title:
                '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${tableIndex + 1}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.rateIsRequired, defaultMessage: '费率必填')}');
        return false;
      }
      if (timePeriodRow.Hm == null) {
        TotastUtils.tip(Get.context!,
            title:
                '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${tableIndex + 1}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.timeIsRequired, defaultMessage: '时间必填')}');
        return false;
      }
      if (index > 0) {
        if (DateTime.parse('2024-01-01 ${timePeriodRow.Hm}').isSameOrBefore(
            DateTime.parse('2024-01-01 ${timePeriod[index - 1].Hm}'))) {
          TotastUtils.tip(Get.context!,
              title:
                  '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${tableIndex + 1}：${index + 1} ${FuncUtils.formatMessage(resKey: ResKey.timeIsGreater, defaultMessage: '时间必须大于')}${timePeriod[index - 1].Hm}');
          return false;
        }
      }
    }
  }

  return true;
}
